from django.db import models

# Create your models here.
from django.db.models import CASCADE

'''
null = True 空值填充为NULL
blank = True 允许为空
'''

class Students(models.Model):
    # """
    #     id    username   password     性别   sfz    StudentsDetail更多详情信息  loginLastTime
    #      1    shenhuaw     123           0    410..       ...Fk                    2020.02.3..
    #      2    张明         123           0    410..       ...Fk                    2020.02.3..
    #      3    李鑫鑫       123           0    410..       ...Fk                    2020.02.3..
    # """

    id = models.AutoField(primary_key=True)
    username = models.CharField(max_length=32,default='')
    password = models.CharField(max_length=64,default='')
    loginLastTime = models.DateTimeField(auto_now_add=True,null=True, blank=True)
    #基础信息
    realName = models.CharField(max_length=32)
    CHOSE_SEX = ((0, '女生'), (1, '男生'))
    userhead = models.ImageField(upload_to='headerimg/%Y/%m/%d',default=None,blank=True,null=True,)
    sex = models.IntegerField(null=True,blank=True,choices=CHOSE_SEX)
    sfz = models.IntegerField(null=True,blank=True,default='',unique=True)

    student_detail = models.OneToOneField(to='StudentsDetail', on_delete=CASCADE,default='',blank=True,null=True)
    student_roles = models.ForeignKey(to='Roles',on_delete=CASCADE,default=None,blank=True,null=True)
    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '主表--学生基本表'
        verbose_name_plural = verbose_name

#学生的详情信息
class StudentsDetail(models.Model):
    """
        id    introduce个人简介     qq号             tel              status       headImg
         1   我心情很好            317909531         18838828864      在校
         2   我们一起玩吧          22555...          138324 ....      已毕业
         3   今天心情不错          22555...          138324 ....      已毕业
    """
    introduce = models.CharField(max_length=200)
    qq = models.IntegerField(null=True, blank=True, default='')
    tel = models.IntegerField(null=True, blank=True, default='')
    status = models.CharField(max_length=64, default='')

    def __str__(self):
        return self.introduce

    class Meta:
        verbose_name = '学生详情表'
        verbose_name_plural = verbose_name


class Comment(models.Model):

    comment = models.CharField(max_length=100,null=True, blank=True, default='')
    # commentMakerId = models.IntegerField(blank=False,default= 0)  # commentMakerid 是主动评论者,表内记录发帖人id
    # 一个评论对于一个学生或者老师,一个老师或学生有多个评论
    #评论表关联着被评论用户
    commentMakerId = models.ForeignKey(to='Students',null=True, blank=True, default='',on_delete=CASCADE, related_name='commentMakerId')   # 被评论用户,被关联着,一个人多个评论条数
    commentToId = models.ForeignKey(to='Students',null=True, blank=True, default='',on_delete=CASCADE, related_name= 'commentToId')   # 被评论用户,被关联着,一个人多个评论条数
    commentTime = models.DateTimeField(auto_now=True,null=True, blank=True)

    def __str__(self):
        print(type(self.commentMakerId))
        return f'{self.commentMakerId.realName}给{self.commentToId.realName}发了一条评论--{self.commentTime}'

    class Meta:
        verbose_name = '评论表'
        verbose_name_plural = verbose_name


#用户授权TOKEN
class UserToken(models.Model):
    userToken_User = models.OneToOneField('Students',on_delete=CASCADE)
    token = models.CharField(max_length=60,null=False,unique=True,default='0')

    def __str__(self):
        return self.token


class Roles(models.Model):
    id = models.AutoField(primary_key=True)
    roleName = models.CharField(max_length=20,null=True,blank=True,default=None)